<template>
	<view class="form-area">
		<u--form labelPosition="top" labelWidth="200" :model="form" :rules="rules" ref="uForm">
			<u-form-item required label="列统情况:" prop="projectBuild.listedStatistics">
				<u-radio-group style="margin-top: 30rpx;" v-model="form.projectBuild.listedStatistics">
					<u-radio shape="circle" :name="1" label="在库"></u-radio>
					<u-radio style="margin: 0 20rpx;" shape="circle" :name="2" label="未入库"></u-radio>
					<u-radio shape="circle" :name="3" label="退库"></u-radio>
				</u-radio-group>
			</u-form-item>
			<u-form-item required label="累计完成投资:" prop="projectBuild.accumulatedCompleted">
				<u--input type="digit" placeholder="请输入累计完成投资" maxlength="10"
					v-model="form.projectBuild.accumulatedCompleted">
					<template slot="suffix">万元</template>
				</u--input>
			</u-form-item>
			<u-form-item required label="当年完成投资:" prop="projectBuild.currentYearCompleted">
				<u--input type="digit" placeholder="请输入当年完成投资" maxlength="10"
					v-model="form.projectBuild.currentYearCompleted">
					<template slot="suffix">万元</template>
				</u--input>
			</u-form-item>
		</u--form>
		<view style="padding: 40rpx 0;">
			<u-button shape='circle' class="sub-btn" type="primary" @click="submit">提交</u-button>
		</view>
	</view>
</template>

<script>
	import {
		subEdit,
		subAdd,
		getProjectInfo
	} from '@/utils/api.js'

	export default {
		data() {
			return {
				form: {
					"projectBuild": {
						baseId: undefined,
						listedStatistics: undefined,
						accumulatedCompleted: undefined,
						currentYearCompleted: undefined
					}
				},
				rules: {
					"projectBuild.listedStatistics": [{
						type: 'number',
						required: true,
						message: "列统情况不能为空",
						trigger: "change"
					}],
					"projectBuild.accumulatedCompleted": [{
						required: true,
						message: "累计完成投资不能为空",
						trigger: "blur"
					}],
					"projectBuild.currentYearCompleted": [{
						required: true,
						message: "当年完成投资不能为空",
						trigger: "blur"
					}]
				},
				projectId:null
			}
		},
		onLoad(option) {
			this.projectId=option.id
			getProjectInfo(option.id).then(res => {
				if(res.data.projectBuild){
					this.form.projectBuild=res.data.projectBuild
				}
			})
		},
		onReady() {
			//如果需要兼容微信小程序，并且校验规则中含有方法等，只能通过setRules方法设置规则。
			this.$refs.uForm.setRules(this.rules)
		},
		methods: {
			submit() {
				console.log('this.form: ', this.form);
				this.$refs.uForm.validate().then(res => {
					uni.showLoading({
						title: '提交中'
					});
					var subForm = {
						projectBuild: this.form.projectBuild
					}
					if (this.form.projectBuild.baseId) {
						subEdit(subForm).then(res => {
							if (res.code == 200) {
								uni.hideLoading()
								uni.showToast({
									title: '提交成功！',
									icon: 'success',
									duration: 2000
								})
								setTimeout(() => {
									uni.navigateBack()
								}, 2000)
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 2000
								})
							}
						})
					} else {
						subAdd(this.projectId,subForm).then(res => {
							if (res.code == 200) {
								uni.hideLoading()
								uni.showToast({
									title: '提交成功！',
									icon: 'success',
									duration: 2000
								})
								setTimeout(() => {
									uni.navigateBack()
								}, 2000)
							} else {
								uni.showToast({
									title: res.msg,
									icon: 'none',
									duration: 2000
								})
							}
						})
					}
				}).catch(errors => {
					console.log('errors: ', errors);
					uni.$u.toast('请完善必填项！')
				})
			}
		}
	}
</script>

<style scoped lang="scss">
	.form-area {
		background-color: #fff;
		margin-top: 30rpx;
		padding: 10rpx 30rpx 30rpx 30rpx;
	}
</style>